// pages/address/address.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Tname: '收货地址',
    status:''
  },

  // onChange(event) {
  //   this.setData({
  //     result: event.detail
  //   });
  //   console.log(event)
  // },


  addressChange(event) {
    let _this=this
    let addressid=event.currentTarget.dataset.addressid.id
    console.log(addressid)
    this.setData({
      checkedAddress: event.detail,
      addressid
    });
    if (event.detail == true) {
      this.setData({
        status: 1
      })
    } else {
      this.setData({
        status: 0
      })
    }
    http.postHttp('/user_address_status',{
      status:_this.data.status,
      id:addressid
    },{
      token:wx.getStorageSync('token')
    }).then(res=>{
      wx.showToast({
        title: '设置成功',
        icon:"none"
      })
      this.onShow()
      // wx.navigateTo({
      //   url: '/pages/address/address',
      // })
    })
  },
  goEditAddress(e) {
    let addressid=e.currentTarget.dataset.addressid.id
    console.log(addressid,'addressid')
    wx.navigateTo({
      url: `/pages/editAddress/editAddress?addressid=${addressid}`,
    })
  },
  goNewAddress(e){
    wx.navigateTo({
      url: '/pages/editAddress/editAddress',
    })
  },
  goDelectAddress(e){
    let that=this
    let addressid=e.currentTarget.dataset.addressid.id
    wx.showModal({
      title: '删除地址',
      content: '确认删除该地址？',
      showCancel: true,//是否显示取消按钮
      cancelText:"取消",//默认是“取消”
      cancelColor:'black',//取消文字的颜色
      confirmText:"确认",//默认是“确定”
      confirmColor: '#999999',//确定文字的颜色
      success: function (res) {
         if (res.cancel) {
            //点击取消,默认隐藏弹框
         } else {
            //点击确定
            // temp.splice(index, 1),
            // that.setData({
            //    tempFilePaths: temp,
            // })
            http.getHttp('/user_address_del',{
              id:addressid
            },{
              token:wx.getStorageSync('token')
            }).then(res=>{
              that.onShow()
              // wx.navigateTo({
              //   url: '/pages/address/address',
              // })
              console.log(res,'删除res')
            })
         }
      }
   })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // http.getHttp('/user_address', {}, {
    //   token: wx.getStorageSync('token')
    // }).then(res => {
    //   let tmpArr = res.data.data
    //   this.setData({
    //     tmp: tmpArr, 
    //   })
    // })
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    http.getHttp('/user_address', {}, {
      token: wx.getStorageSync('token')
    }).then(res => {
      let tmpArr = res.data.data
      this.setData({
        tmp: tmpArr, 
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})